﻿using System;
using System.Collections.Generic;
using System.Text;
using CGLib.Enums;

namespace CGLib
{
    public static class Parsers
    {
        /// <summary>
        /// Get the integer value of the card, relying on the highest and lowest card of the game.
        /// </summary>
        /// <param name="game">The game to use, for comparing the card's value.</param>
        /// <param name="value">The value to parse.</param>
        public static int GetGCV(IGame game, Value value)
        {
            if (value == game.LowestCard())
                return 1;

            if (value > game.LowestCard())
                return (int)value - (int)game.LowestCard() + 1;
            else
                return 13 - ((int)game.HighestCard() - (int)value);
        }

        public static void SortCards(IGame game, List<Card> cards)
        {
            cards.Sort(delegate(Card c1, Card c2)
            {
                if (Parsers.GetGCV(game, c1.Value).CompareTo(Parsers.GetGCV(game, c2.Value)) == 0)
                    return c1.Suite.CompareTo(c2.Suite);
                else return Parsers.GetGCV(game, c1.Value).CompareTo(Parsers.GetGCV(game, c2.Value));
            });
        }

        /// <summary>
        /// Get an integer value of the sum of the cards suites, with a card list.
        /// </summary>
        public static int GetSuiteSum(List<Card> cardList)
        {
            int sum = 0;

            for (int i = 0; i < cardList.Count; i++)
            {
                if (cardList[i].Suite == Suite.Spade)
                    sum += 1;
                else if (cardList[i].Suite == Suite.Club)
                    sum += 3;
                else if (cardList[i].Suite == Suite.Diamond)
                    sum += 6;
                else if (cardList[i].Suite == Suite.Heart)
                    sum += 10;
            }

            return sum;
        }

        /// <summary>
        /// Get an integer value of the sum of the cards suites, with an array of cards.
        /// </summary>
        public static int GetSuiteSum(Card[] cardList)
        {
            return GetSuiteSum(new List<Card>(cardList));
        }
    }
}
